package com.example.memurbac_service.dao;

import com.example.memurbac_service.pojo.Memu;
import com.example.memurbac_service.pojo.Memu;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MemuMapper
{

    /**
     * 添加菜单
     */
    @Insert("insert into t_memu values(#{id},#{memu_name},#{memu_desc},#{memu_status},now(),now(),#{memu_style},#{memu_picture},#{memu_url},#{memu_typeid},#{memu_skiptype},#{memu_grade},#{memu_parentid},#{memu_logname},#{memu_logurl},#{memu_logpicture})")
    int addMemu(Memu memu);

    /**
     * 修改菜单
     */
    @Update("update t_memu set memu_name=#{memu_name},memu_desc=#{memu_desc},memu_modiftime=now(),memu_style=#{memu_style},memu_picture=#{memu_picture},memu_url=#{memu_url},memu_typeid=#{memu_typeid},memu_skiptype=#{memu_skiptype},memu_grade=#{memu_grade},memu_parentid=#{memu_parentid},memu_logname=#{memu_logname},memu_logurl=#{memu_logurl},memu_logpicture=#{memu_logpicture} where id=#{id}")
    int upteMemu(Memu memu);

    /**
     * 删除菜单
     */
    @Delete("delete from  t_memu  where id=#{memuid}")
    int delMemu(int memuid);

    /**
     * 停用菜单
     */
    @Update("update t_memu set memu_status=1 where id=#{memuid}")
    int offMemu(int memuid);

    /**
     * 启用菜单
     */
    @Update("update t_memu set memu_status=0 where id=#{memuid}")
    int onMemu(int memuid);

    /**
     * 根据id查询菜单数据
     */
    @Select("select * from t_memu where  id=#{memuid} ")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "memu_name",column = "memu_name"),
            @Result(property = "memu_desc",column = "memu_desc"),
            @Result(property = "memu_status",column = "memu_status"),
            @Result(property = "memu_createtime",column = "memu_createtime"),
            @Result(property = "memu_modiftime",column = "memu_modiftime"),
            @Result(property = "memu_style",column = "memu_style"),
            @Result(property = "memu_picture",column = "memu_picture"),
            @Result(property = "memu_url",column = "memu_url"),
            @Result(property = "memu_typeid",column = "memu_typeid"),
            @Result(property = "memu_skiptype",column = "memu_skiptype"),
            @Result(property = "memu_grade",column = "memu_grade"),
            @Result(property = "memu_parentid",column = "memu_parentid"),
            @Result(property = "memu_logname",column = "memu_logname"),
            @Result(property = "memu_logurl",column = "memu_logurl"),
            @Result(property = "memu_logpicture",column = "memu_logpicture")
    })
    Memu selectidMemu(int memuid);

    /**
     * 模糊查询菜单 总查询
     */
    @Select("select a.*,b.mt_name  from t_memu a,t_memutype b where a.memu_typeid=b.id  and  memu_typeid like '%${typeid}%'  ")
    @Results({

            @Result(property = "id",column = "id"),
            @Result(property = "memu_name",column = "memu_name"),
            @Result(property = "memu_desc",column = "memu_desc"),
            @Result(property = "memu_status",column = "memu_status"),
            @Result(property = "memu_createtime",column = "memu_createtime"),
            @Result(property = "memu_modiftime",column = "memu_modiftime"),
            @Result(property = "memu_style",column = "memu_style"),
            @Result(property = "memu_picture",column = "memu_picture"),
            @Result(property = "memu_url",column = "memu_url"),
            @Result(property = "memu_typename",column = "mt_name"),
            @Result(property = "memu_skiptype",column = "memu_skiptype"),
            @Result(property = "memu_grade",column = "memu_grade"),
            @Result(property = "memu_parentid",column = "memu_parentid"),
            @Result(property = "memu_logname",column = "memu_logname"),
            @Result(property = "memu_logurl",column = "memu_logurl"),
            @Result(property = "memu_logpicture",column = "memu_logpicture")


    })
    List<Memu> reachMemu(String typeid);
    /**
     * 根据用户id和类型id查询该用户的某个大菜单 树形
     */
    @Select("select * from (select a.*,b.mt_name  from t_memu a,t_memutype b,user_role d,role_auth e,auth_memu f where d.role_id= e.role_id and e.auth_id=f.auth_id and f.memu_id=a.id and a.memu_typeid=b.id and a.memu_status=0  and   d.user_id=#{userid}) a where a.memu_typeid=#{typeid} order by memu_grade asc")
    @Results({

            @Result(property = "id",column = "id"),
            @Result(property = "memu_name",column = "memu_name"),
            @Result(property = "memu_desc",column = "memu_desc"),
            @Result(property = "memu_status",column = "memu_status"),
            @Result(property = "memu_createtime",column = "memu_createtime"),
            @Result(property = "memu_modiftime",column = "memu_modiftime"),
            @Result(property = "memu_style",column = "memu_style"),
            @Result(property = "memu_picture",column = "memu_picture"),
            @Result(property = "memu_url",column = "memu_url"),
            @Result(property = "memu_typename",column = "mt_name"),
            @Result(property = "memu_skiptype",column = "memu_skiptype"),
            @Result(property = "memu_grade",column = "memu_grade"),
            @Result(property = "memu_parentid",column = "memu_parentid"),
            @Result(property = "memu_logname",column = "memu_logname"),
            @Result(property = "memu_logurl",column = "memu_logurl"),
            @Result(property = "memu_logpicture",column = "memu_logpicture")


    })
    List<Memu> reachbyuseridMemu(int userid,int typeid);

    /**
     * 按照菜单类型id查询整个菜单,开放给其他服务的接口   树级
     */
    @Select("select a.*,b.mt_name  from t_memu a,t_memutype b where  a.memu_typeid=b.id  and a.memu_status=0  and memu_typeid=#{typeid} order by a.memu_grade asc")
    @Results({

            @Result(property = "id",column = "id"),
            @Result(property = "memu_name",column = "memu_name"),
            @Result(property = "memu_desc",column = "memu_desc"),
            @Result(property = "memu_status",column = "memu_status"),
            @Result(property = "memu_createtime",column = "memu_createtime"),
            @Result(property = "memu_modiftime",column = "memu_modiftime"),
            @Result(property = "memu_style",column = "memu_style"),
            @Result(property = "memu_picture",column = "memu_picture"),
            @Result(property = "memu_url",column = "memu_url"),
            @Result(property = "memu_typename",column = "mt_name"),
            @Result(property = "memu_skiptype",column = "memu_skiptype"),
            @Result(property = "memu_grade",column = "memu_grade"),
            @Result(property = "memu_parentid",column = "memu_parentid"),
            @Result(property = "memu_logname",column = "memu_logname"),
            @Result(property = "memu_logurl",column = "memu_logurl"),
            @Result(property = "memu_logpicture",column = "memu_logpicture")


    })
    List<Memu> reachbytypeidMemu(int typeid);

    /**
     * 查询用户id对应所有菜单的类型id
     */
    @Select("select a.memu_typeid  from t_memu a,t_memutype b,user_role d,role_auth e,auth_memu f  where d.role_id= e.role_id and e.auth_id=f.auth_id and f.memu_id=a.id and a.memu_typeid=b.id and a.memu_status=0 and d.user_id=#{userid} group by  a.memu_typeid ")
    List<Integer> reachtypeidMemu(int userid);






}
